package com.guigu.dh.controller;

import com.guigu.dh.common.result.Result;
import com.guigu.dh.pojo.cmn.Dict;
import com.guigu.dh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据id查询子节点数据列表")
    @PostMapping("/findChildrenData/{id}")
    public Result findChildrenData(@PathVariable Long id){
        List<Dict> list = dictService.findChildren(id);
        return Result.ok(list);
    }

    @ApiOperation(value = "导出数据的操作")
    @GetMapping("/exportData")
    public void exportData (HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation(value = "导入数据的操作")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation(value = "根据dictCode和value查询")
    @GetMapping("/findDictByValue/{dictCode}/{value}")
    public Result findDictByValue (@PathVariable String dictCode,
                                   @PathVariable String value){
        return dictService.findDictByValue(dictCode,value);
    }

    @ApiOperation(value = "根据dictCode查询")
    @GetMapping("/findDictByDictCode/{dictCode}")
    public Result findDictByDictCode(@PathVariable String dictCode){
        return dictService.findDictByDictCode(dictCode);
    }
}
